//
//  EvaluationCell.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/30.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "EvaluationCell.h"
#import <Masonry.h>
#import "EvaluationModel.h"
@interface EvaluationCell ()
/**
 *  头像
 */
@property (nonatomic ,weak) UIImageView *iconView;
/**
 *  昵称
 */
@property (nonatomic ,weak) UILabel *nameLabel;
/**
 *  星级
 */
@property (nonatomic ,weak) UIImageView *starView;
/**
 *  正文
 */
@property (nonatomic ,weak) UILabel *contentLabel;
/**
 *  配图
 */
@property (nonatomic ,weak) UIImageView *pictureView;
/**
 *  时间
 */
@property (nonatomic ,weak) UILabel *timeLabel;
@end

@implementation EvaluationCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    static NSString *identifier = @"EvaluationCell";
    EvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[EvaluationCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {

    // 头像
    UIImageView *icon = [[UIImageView alloc] init];
    icon.layer.masksToBounds = YES;
    icon.layer.cornerRadius = icon.frame.size.width /2;
    [self.contentView addSubview:icon];
    self.iconView = icon;
    
    // 姓名
    UILabel *name = [UILabel setLabelWithText:@"乔帮主" fontSize:11 color:[UIColor colorWithHexString:@"#333333"]];
    [self.contentView addSubview:name];
    self.nameLabel = name;
    
    // 星级
    UIImageView *star = [[UIImageView alloc] init];
    star.image = [UIImage imageNamed:@"wuxing"];
    [self.contentView addSubview:star];
    self.starView = star;
    
    // 时间
    UILabel *time = [UILabel setLabelWithText:@"2017-03-08" fontSize:11 color:color_textTwo];
    [self.contentView addSubview:time];
    self.timeLabel = time;
    
    // 文字内容
    UILabel *content = [UILabel setLabelWithText:@"速度快干哈控股哈坎工行卡干哈控股哈开个卡高考改革可哈根埃里克好感动看后感乐山大佛更加了解拉都放假了家里蹲过" fontSize:12 color:color_textOne];
    content.numberOfLines = 2;
    content.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:content];
    self.contentLabel = content;
    
    
    // 图片
    UIImageView *picture = [[UIImageView alloc] init];
    [self.contentView addSubview:picture];
    self.pictureView = picture;
    
    // 底部线条
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];
    [self.contentView addSubview:line];
    
    // 设置头像约束
    icon.image = [UIImage imageNamed:@"gerenzhongxin"];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset = 8;
        make.top.offset = 10;
        make.width.height.offset(28);
    }];
    
    // 设置姓名约束
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon);
        make.left.equalTo(icon.mas_right).offset(8);
    }];
    
    // 设置星级约束
    [star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).offset(4);
        make.left.equalTo(icon.mas_right).offset(8);
        make.width.offset(48);
        make.height.offset(8);
    }];
    
    // 设置时间约束
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name);
        make.right.offset(-8);
    }];
    
    // 设置文字内容约束
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(8);
        make.top.equalTo(star.mas_bottom).offset(6);
        make.right.offset(-8);
    }];
    
    // 设置图片约束
    picture.image = [UIImage imageNamed:@"占位图-1"];
    [picture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(8);
        make.top.equalTo(content.mas_bottom).offset(12);
        make.width.height.offset(48);
    }];
    
}

- (void)setModel:(EvaluationModel *)model {

    _model = model;
    
    self.iconView.image = [UIImage imageNamed:model.icon];
    self.nameLabel.text = model.name;
    self.timeLabel.text = model.time;
    self.contentLabel.text = model.content;
    
    if (model.picture) {
        UIImage *image = [UIImage imageNamed:model.picture];
        

        CGFloat width = 100 / image.size.height * image.size.height;
        _pictureView.image = image;
  
        
        [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
        }];
        
    }else {
        _pictureView.image = nil;
        
    }
    
}

@end
